package com.qf.service;

import com.qf.constant.CacheConstants;
import com.qf.constant.OperatorConstants;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import com.qf.util.CheckMobileOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.lang.annotation.ElementType;
import java.util.Map;

/**
 * 号段补全策略实现类  根据发送的目标手机号识别运营商   识别归属地
 */
@Service(value = "phase")
@Slf4j
public class PhaseServiceImpl implements  FilerChainService {

    @Autowired
    private CacheFeign cacheFeign;
    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        log.info("[策略模块 - 号段补全策略]，开始执行，StandardSubmit={}",standardSubmit);

        //1. 获取手机号
        String mobile = standardSubmit.getDestMobile();

//        2. 根据目标手机号判断手机对应的运营商（正则表达式）
        if(CheckMobileOperator.isChinaMobilePhoneNum(mobile)){
            //手机号是移动
            //运营商ID
            standardSubmit.setOperatorId(OperatorConstants.CHINA_MOBILE);
            //下发网关ID  通道ID
            standardSubmit.setGatewayID(OperatorConstants.CHINA_MOBILE);
        }else if (CheckMobileOperator.isChinaUnicomPhoneNum(mobile)){
            //手机号是联通
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNICOM);
            standardSubmit.setGatewayID(OperatorConstants.CHINA_UNKNOWN);
        }else if (CheckMobileOperator.isChinaTelecomPhoneNum(mobile)){
            //手机号是电信
            standardSubmit.setOperatorId(OperatorConstants.CHINA_TELECOM);
            standardSubmit.setGatewayID(OperatorConstants.CHINA_TELECOM);
        }else {
            //未知
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNKNOWN);
            standardSubmit.setGatewayID(OperatorConstants.CHINA_UNKNOWN);
        }


        /**
         *  3. 根据目标手机号 和Redis中获取到的号码数据  判断出号码的归属地 省市
         */
        //3.1 封装key  目标手机号前七位截取
        String key = CacheConstants.CACHE_PREFIX_PHASE + mobile.substring(0, 7);
        //3.2. 根据key查询数据
        String value = cacheFeign.get(key);
        if (StringUtils.isEmpty(value)){
            log.info("[策略模块 - 号码补全策略]，目标手机号有误，查询不到归属地，目标手机号={}",mobile);
            return false;
        }
        //3.3. 处理value值, 省id, 市id
        String[] proviceAndCity = value.split(",");
        if(proviceAndCity.length != 2){
            log.info("[策略模块 - 号码补全策略]，目标手机号有误，查询不到归属地，目标手机号={}",mobile);
            return false;
        }

        standardSubmit.setProvinceId(Integer.parseInt(proviceAndCity[0]));
        standardSubmit.setCityId(Integer.parseInt(proviceAndCity[1]));

        /**
         * 4. 获取Redis中路由数据
         */
        Map<String,Object> routerMap = cacheFeign.hMGet(CacheConstants.CACHE_PREFIX_ROUTER + standardSubmit.getClientID() + ":" + standardSubmit.getGatewayID());

        /**
         * 5. 获取Redis中的通道数据
         */
        Map<String, Object> channelMap = cacheFeign.hMGet(CacheConstants.CACHE_PREFIX_SMS_CHANNEL + standardSubmit.getOperatorId() + ":" + standardSubmit.getGatewayID());

        /**
         *6. 根据路由数据和通道数据 ，以及目标手机号 匹配出短信的下发源号码，以及当前短信的费用
         */
        if (routerMap != null && channelMap != null){
            //账户接入号
            String spnumber = String.valueOf(channelMap.get("spnumber"));
            //扩展号
            String extendnumber =  String.valueOf(routerMap.get("extendnumber"));
            //下发的源号码
            String srcNumber = spnumber + extendnumber;
            standardSubmit.setSrcNumber(srcNumber);
            //当前短信费用
            standardSubmit.setFee(Long.parseLong(String.valueOf(routerMap.get("price"))));
        }

        log.info("[策略模块 - 号段补全策略] 策略执行成功，号段补全完成 StandardSubmit={}",standardSubmit);
        return true;
    }
}
